<?php
App::uses('AppController', 'Controller');
/**
 * PaidAds Controller
 *
 * @property PaidAd $PaidAd
 * @property PaginatorComponent $Paginator
 */
class PaidAdsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator','Session');
        public $helpers = array('Form', 'Html', 'Js', 'Time','Session');
        
public function beforeFilter() {
        $this->Auth->allow('index', 'add', 'edit', 'expired');
    }

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->PaidAd->recursive = 0;
		$this->set('paidAds', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->PaidAd->exists($id)) {
			throw new NotFoundException(__('Invalid paid ad'));
		}
		$options = array('conditions' => array('PaidAd.' . $this->PaidAd->primaryKey => $id));
		$this->set('paidAd', $this->PaidAd->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
            if ($this->Auth->user('role')=='Admin' || $this->Auth->user('role')=='Register'){ 
		if ($this->request->is('post')) {
			$this->PaidAd->create();
                        $validate = true;
                        $allowedMime = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/jpg');

                        // Control Picture of Before
                        $requestimage = $this->request->data['PaidAd']['banner'];
                        if (!empty($requestimage['type']) && in_array($requestimage['type'], $allowedMime)) {
                            $newfilename = date("YmdGis") . $requestimage['name'];
                            if (!move_uploaded_file($requestimage['tmp_name'], WWW_ROOT . 'img' . DS . 'advertises' . DS . $newfilename)) {
                                $this->PaidAd->invalidate('banner', 'File was not uploaded, please try again.');
                                $validate = false;
                            } else {
                                $this->request->data['PaidAd']['banner'] = $newfilename;
                            }
                        } else {
                            if (!empty($requestimage['tmp_name'])) {
                                $this->PaidAd->invalidate('banner', 'Please supply a valid image (.jpg, .gif or .png)');
                                $validate = false;
                            } else {
                                $this->request->data['PaidAd']['banner'] = NULL;
                            }
                        }
                        // Expired Date
                        $numMonth = $this->request->data['PaidAd']['expired'];
                        $myDate = date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+$numMonth month" ) );
                        $this->request->data['PaidAd']['expired'] = $myDate;
                        $this->request->data['PaidAd']['month'] = $numMonth;
                        
			if ($validate && $this->PaidAd->saveAssociated($this->request->data)) {
				$this->Session->setFlash(__('The paid ad has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The paid ad could not be saved. Please, try again.'));
			}
		}
		$paidUsers = $this->PaidAd->PaidUser->find('list');
		$this->set(compact('paidUsers'));
            } else{
                 return $this->redirect(array(
                                'controller' => 'posts',
                                'action' => 'index'));
            }
	}
        
/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
            Controller::loadModel('Paid_ads');
		if (!$this->PaidAd->exists($id)) {
			throw new NotFoundException(__('Invalid paid ad'));
		}
                $oldPaidads = $this->PaidAd->find('all', array('conditions'=>array('PaidAd.id'=>$id)));
		if ($this->request->is(array('post', 'put'))) {
                    $this->PaidAd->create();
                    $allowedMime = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/jpg');
                        // Control Picture of Before
                        $requestimage = $this->request->data['PaidAd']['banners'];
                        $newfilename = date("YmdGis") . $requestimage['name'];
                        if(!empty($requestimage['name'])) {
                            $image1 = $newfilename;
                            move_uploaded_file($requestimage['tmp_name'], WWW_ROOT . 'img' . DS . 'advertises' . DS . $newfilename);
                            $imageade = new File(WWW_ROOT ."/img/advertises/".$oldPaidads[0]['PaidAd']['banner']);
                            $imageade->delete();
                        }else {
                            $image1 = $oldPaidads[0]['PaidAd']['banner'];
                        }
                        $this->PaidAd->updateAll(
                                array('PaidAd.banner' => "'".$image1."'"),
                                array( 'PaidAd.id' => $id)
                                );
                        // Expired Date
                        $numMonth = $this->request->data['PaidAd']['expired'];
                        $myDate = date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+$numMonth month" ) );
                        $this->request->data['PaidAd']['expired'] = $myDate;
                        $this->request->data['PaidAd']['month'] = $numMonth;
			if ($this->PaidAd->save($this->request->data)) {
				$this->Session->setFlash(__('The paid ad has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The paid ad could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('PaidAd.' . $this->PaidAd->primaryKey => $id));
			$this->request->data = $this->PaidAd->find('first', $options);
		}
		$paidUsers = $this->PaidAd->PaidUser->find('list');
		$this->set(compact('paidUsers'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->PaidAd->id = $id;
		if (!$this->PaidAd->exists()) {
			throw new NotFoundException(__('Invalid paid ad'));
		}
                $oldadsimage = $this->PaidAd->find('all', array('conditions'=>array('PaidAd.id'=>$id)));
		$this->request->onlyAllow('post', 'delete');
		if ($this->PaidAd->delete()) {
			$this->Session->setFlash(__('The paid ad has been deleted.'));
                        $imageade = new File(WWW_ROOT ."/img/advertises/".$oldadsimage[0]['PaidAd']['banner']);
                        $imageade->delete();
		} else {
			$this->Session->setFlash(__('The paid ad could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
        /**
 * expird date method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function expired($id = null) {

		Controller::loadModel('Paid_ads');
		if (!$this->PaidAd->exists($id)) {
			throw new NotFoundException(__('Invalid paid ad'));
		}
                $oldPaidads = $this->PaidAd->find('all', array('conditions'=>array('PaidAd.id'=>$id)));
		// Expired Date
                $numMonth = $oldPaidads[0]['PaidAd']['month'];
                $myDate = date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+$numMonth month" ) );
               $this->PaidAd->updateAll(
                    array('PaidAd.expired' => "'".$myDate."'"),
                    array( 'PaidAd.id' => $id)
                );
                return $this->redirect(array('action' => 'index'));
	}
}
